﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using RM.WinClient.RMServices;
using System.IO;
using RM.WinClient.Utilities;

namespace RM.WinClient
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Finish : Page
    {
        #region Declaration
        Test gblTest;
        #endregion

        #region Constructor

        public Finish()
        {
            InitializeComponent();
        }

        public Finish(Test objTest)
        {
            InitializeComponent();
            lblCandidateName.Content = objTest.Candidate.FullName;
            lblTimeTaken.Content = Util.FormatTime(objTest.Duration);
            gblTest = objTest;
        }

        #endregion

        #region Logout Color

        private void ChangeLogoutColor(object sender, System.Windows.Input.MouseEventArgs e)
        {
            //Logout.Foreground = new SolidColorBrush(Color.FromArgb(255, 9, 38, 224));
        }

        private void RevertLogoutColor(object sender, System.Windows.Input.MouseEventArgs e)
        {
           // Logout.Foreground = new SolidColorBrush(Color.FromArgb(255, 50, 50, 50));
        }

        #endregion

        #region Browse & Upload
        private void btnSelect_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog filedialog = new OpenFileDialog();
            filedialog.Filter = "Zip Files|*.Zip*";
            if (filedialog.ShowDialog().ToString() == "OK")
                txtPath.Text = filedialog.FileName;
        }
        #endregion

        #region Complete the Process

        private void btnFinish_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(txtPath.Text))
            {
                ErrorDialog.Show(Constants.ErrSelectZipFile);
                return;
            }

            using (TestServiceClient obj = new TestServiceClient())
            {
                if (File.Exists(txtPath.Text))
                {
                    using (FileStream stream = File.OpenRead(txtPath.Text))
                    {
                        byte[] content = null;
                        if (stream != null)
                        {
                            content = new byte[stream.Length];
                            stream.Read(content, 0, (int)stream.Length - 1);
                            gblTest.SolutionFile = content;
                       }
                    }
                }
                gblTest.CreatedDate = DateTime.Now;
                gblTest.ModifiedDate = DateTime.Now;
                obj.Test_Save(gblTest);
            }
            //System.Windows.MessageBox.Show("Data Updated Successfully!!!", "Finish", MessageBoxButton.OK);
            MessageDialog.Show("Data Updated Successfully!!!");
            System.Windows.Application.Current.Shutdown();
        }

        #endregion

    }
}
